home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / gfx / show / gs_src_gs.lha / gs5.03 / gdevdjet.c < prev    next >
C/C++ Source or Header  |  1997-01-01  |  20KB  |  607 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevdjet.c */
  20. /* HP LaserJet/DeskJet driver for Ghostscript */
  21. #include "gdevprn.h"
  22. #include "gdevpcl.h"
  23.  
  24. /*
  25.  * Thanks for various improvements to:
  26.  *    Jim Mayer (mayer@wrc.xerox.com)
  27.  *    Jan-Mark Wams (jms@cs.vu.nl)
  28.  *    Frans van Hoesel (hoesel@chem.rug.nl)
  29.  *    George Cameron (g.cameron@biomed.abdn.ac.uk)
  30.  *    Nick Duffek (nsd@bbc.com)
  31.  * Thanks for the LJIIID duplex capability to:
  32.  *    PDP (Philip) Brown (phil@3soft-uk.com)
  33.  * Thanks for the OCE 9050 driver to:
  34.  *    William Bader (wbader@EECS.Lehigh.Edu)
  35.  */
  36.  
  37. /*
  38.  * You may select a default resolution of 75, 100, 150, 300, or
  39.  * (LJ4 only) 600 DPI in the makefile, or an actual resolution on
  40.  * the gs command line.
  41.  *
  42.  * If the preprocessor symbol A4 is defined, the default paper size is
  43.  * the European A4 size; otherwise it is the U.S. letter size (8.5"x11").
  44.  *
  45.  * To determine the proper "margin" settings for your printer, see the
  46.  * file align.ps.
  47.  */
  48.  
  49. /* Define the default, maximum resolutions. */
  50. #ifdef X_DPI
  51. #  define X_DPI2 X_DPI
  52. #else
  53. #  define X_DPI 300
  54. #  define X_DPI2 600
  55. #endif
  56. #ifdef Y_DPI
  57. #  define Y_DPI2 Y_DPI
  58. #else
  59. #  define Y_DPI 300
  60. #  define Y_DPI2 600
  61. #endif
  62.  
  63. /*
  64.  * For all DeskJet Printers:
  65.  *
  66.  *  Maximum printing width               = 2400 dots = 8"
  67.  *  Maximum recommended printing height  = 3100 dots = 10 1/3"
  68.  *
  69.  * All Deskjets have 1/2" unprintable bottom margin.
  70.  * The recommendation comes from the HP Software Developer's Guide for
  71.  * the DeskJet 500, DeskJet PLUS, and DeskJet printers, version C.01.00
  72.  * of 12/1/90.
  73.  *
  74.  * Note that the margins defined just below here apply only to the DeskJet;
  75.  * the paper size, width and height apply to the LaserJet as well.
  76.  */
  77.  
  78. /* Margins are left, bottom, right, top. */
  79. /* from Frans van Hoesel hoesel@rugr86.rug.nl. */
  80. /* A4 has a left margin of 1/8 inch and at a printing width of
  81.  * 8 inch this give a right margin of 0.143. The 0.09 top margin is
  82.  * not the actual margin - which is 0.07 - but compensates for the
  83.  * inexact paperlength which is set to 117 10ths.
  84.  * Somebody should check for letter sized paper. I left it at 0.07".
  85.  */
  86. #define DESKJET_MARGINS_LETTER  0.2, 0.45, 0.3, 0.05
  87. #define DESKJET_MARGINS_A4    0.125, 0.5, 0.143, 0.09
  88. /* Similar margins for the LaserJet, */
  89. /* from Eddy Andrews eeandrew@pyr.swan.ac.uk: */
  90. #define LASERJET_MARGINS_A4    0.25, 0.20, 0.25, 0.00
  91. /* the following from Bob Atkins bob@digilink.net are WRONG: */
  92. /*#define LASERJET_MARGINS_A4    0.25, 0.0, 0.25, 0.0*/
  93. /* from Dale Atems atems@igor.physics.wayne.edu: */
  94. /*#define LASERJET_MARGINS_LETTER    0.275, -0.06, 0.425, 0.26*/
  95. /* ... superseded by the following from Bob Atkins bob@digilink.net: */
  96. #define LASERJET_MARGINS_LETTER    0.25, 0.0, 0.25, 0.0
  97.  
  98. /* The number of blank lines that make it worthwhile to reposition */
  99. /* the cursor. */
  100. #define MIN_SKIP_LINES 7
  101.  
  102. /* We round up the LINE_SIZE to a multiple of a ulong for faster scanning. */
  103. #define W sizeof(word)
  104.  
  105. /* Printer types */
  106. #define LJ    0
  107. #define LJplus    1
  108. #define LJ2p    2
  109. #define LJ3    3
  110. #define DJ    4
  111. #define DJ500    5
  112. #define LJ4    6
  113. #define LP2563B    7
  114. #define LJ3D    8
  115. #define    OCE9050    9
  116.  
  117. /*
  118.  * The notion that there is such a thing as a "PCL printer" is a fiction:
  119.  * no two "PCL" printers, even at the same PCL level, have identical
  120.  * command sets.  The command strings below were established by hearsay
  121.  * and by trial and error.  (The H-P documentation isn't fully accurate
  122.  * either; for example, it doesn't reveal that the DeskJet printers
  123.  * implement anything beyond PCL 3.)
  124.  */
  125.  
  126. /* Printer capabilities */
  127. typedef enum {
  128.     mode_0,        /* PCL 3, use <ESC>*p+<n>Y for vertical spacing */
  129.     mode_0ns,    /* PCL 3 but no vertical spacing */
  130.     mode_2,        /* PCL 4, use <ESC>*b<n>Y for vertical spacing */
  131.     mode_2p,    /* PCL 4 but no vertical spacing */
  132.     mode_3,        /* PCL 5, use <ESC>*b<n>Y and clear seed row */
  133.             /* (includes mode 2) */
  134.     mode_3ns    /* PCL 5 but no vertical spacing */
  135. } compression_modes;
  136.  
  137. /* The device descriptors */
  138. private dev_proc_open_device(hpjet_open);
  139. private dev_proc_close_device(hpjet_close);
  140. private dev_proc_print_page(djet_print_page);
  141. private dev_proc_print_page(djet500_print_page);
  142. private dev_proc_print_page(ljet_print_page);
  143. private dev_proc_print_page(ljetplus_print_page);
  144. private dev_proc_print_page(ljet2p_print_page);
  145. private dev_proc_print_page(ljet3_print_page);
  146. private dev_proc_print_page(ljet3d_print_page);
  147. private dev_proc_print_page(ljet4_print_page);
  148. private dev_proc_print_page(lp2563_print_page);
  149. private dev_proc_print_page(oce9050_print_page);
  150.  
  151. private gx_device_procs prn_hp_procs =
  152.   prn_params_procs(hpjet_open, gdev_prn_output_page, hpjet_close,
  153.            gdev_prn_get_params, gdev_prn_put_params);
  154.  
  155. gx_device_printer far_data gs_deskjet_device =
  156.   prn_device(prn_hp_procs, "deskjet",
  157.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  158.     X_DPI, Y_DPI,
  159.     0, 0, 0, 0,        /* margins filled in by hpjet_open */
  160.     1, djet_print_page);
  161.  
  162. gx_device_printer far_data gs_djet500_device =
  163.   prn_device(prn_hp_procs, "djet500",
  164.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  165.     X_DPI, Y_DPI,
  166.     0, 0, 0, 0,        /* margins filled in by hpjet_open */
  167.     1, djet500_print_page);
  168.  
  169. gx_device_printer far_data gs_laserjet_device =
  170.   prn_device(prn_hp_procs, "laserjet",
  171.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  172.     X_DPI, Y_DPI,
  173.     0.05, 0.25, 0.55, 0.25,        /* margins */
  174.     1, ljet_print_page);
  175.  
  176. gx_device_printer far_data gs_ljetplus_device =
  177.   prn_device(prn_hp_procs, "ljetplus",
  178.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  179.     X_DPI, Y_DPI,
  180.     0.05, 0.25, 0.55, 0.25,        /* margins */
  181.     1, ljetplus_print_page);
  182.  
  183. gx_device_printer far_data gs_ljet2p_device =
  184.   prn_device(prn_hp_procs, "ljet2p",
  185.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  186.     X_DPI, Y_DPI,
  187.     0.20, 0.25, 0.25, 0.25,        /* margins */
  188.     1, ljet2p_print_page);
  189.  
  190. gx_device_printer far_data gs_ljet3_device =
  191.   prn_device(prn_hp_procs, "ljet3",
  192.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  193.     X_DPI, Y_DPI,
  194.     0.20, 0.25, 0.25, 0.25,        /* margins */
  195.     1, ljet3_print_page);
  196.  
  197. gx_device_printer far_data gs_ljet3d_device =
  198.   prn_device(prn_hp_procs, "ljet3d",
  199.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  200.     X_DPI, Y_DPI,
  201.     0.20, 0.25, 0.25, 0.25,        /* margins */
  202.     1, ljet3d_print_page);
  203.  
  204. gx_device_printer far_data gs_ljet4_device =
  205.   prn_device(prn_hp_procs, "ljet4",
  206.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  207.     X_DPI2, Y_DPI2,
  208.     0, 0, 0, 0,            /* margins */
  209.     1, ljet4_print_page);
  210.   
  211. gx_device_printer far_data gs_lp2563_device =
  212.   prn_device(prn_hp_procs, "lp2563",
  213.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  214.     X_DPI, Y_DPI,
  215.     0, 0, 0, 0,            /* margins */
  216.     1, lp2563_print_page);
  217.  
  218. gx_device_printer far_data gs_oce9050_device =
  219.   prn_device(prn_hp_procs, "oce9050",
  220.     24 * 10, 24 * 10,        /* 24 inch roll (can print 32" also) */
  221.     400, 400,            /* 400 dpi */
  222.     0, 0, 0, 0,            /* margins */
  223.     1, oce9050_print_page);
  224.  
  225. /* Forward references */
  226. private int hpjet_print_page(P6(gx_device_printer *pdev, FILE *prn_stream,
  227.                 int ptype, int dots_per_inch,
  228.                 compression_modes cmodes,
  229.                 const char *page_init));
  230.  
  231. #define ppdev ((gx_device_printer *)pdev)
  232.  
  233. /* Open the printer, adjusting the margins if necessary. */
  234. private int
  235. hpjet_open(gx_device *pdev)
  236. {    /* Change the margins if necessary. */
  237.     const float _ds *m = 0;
  238.  
  239.     if ( ppdev->printer_procs.print_page == djet_print_page ||
  240.          ppdev->printer_procs.print_page == djet500_print_page
  241.        )
  242.     {    static const float m_a4[4] = { DESKJET_MARGINS_A4 };
  243.         static const float m_letter[4] = { DESKJET_MARGINS_LETTER };
  244.         m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? m_a4 :
  245.              m_letter);
  246.     }
  247.     else if ( ppdev->printer_procs.print_page == oce9050_print_page ||
  248.           ppdev->printer_procs.print_page == lp2563_print_page
  249.         )
  250.       ;
  251.     else        /* LaserJet */
  252.     {    static const float m_a4[4] = { LASERJET_MARGINS_A4 };
  253.         static const float m_letter[4] = { LASERJET_MARGINS_LETTER };
  254.         m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? m_a4 :
  255.              m_letter);
  256.     }
  257.     if ( m != 0 )
  258.       gx_device_set_margins(pdev, m, true);
  259.     /* If this is a LJIIID, enable Duplex. */
  260.     if ( ppdev->printer_procs.print_page == ljet3d_print_page )
  261.       ppdev->Duplex = true, ppdev->Duplex_set = 0;
  262.     return gdev_prn_open(pdev);
  263. }
  264.  
  265. /* hpjet_close is only here to eject odd numbered pages in duplex mode. */
  266. private int
  267. hpjet_close(gx_device *pdev)
  268. {    if ( ppdev->Duplex_set >= 0 && ppdev->Duplex )
  269.       {    gdev_prn_open_printer(pdev, 1);
  270.         fputs("\033&l0H", ppdev->file) ;
  271.       }
  272.     return gdev_prn_close(pdev);
  273. }
  274.  
  275. #undef ppdev
  276.  
  277. /* ------ Internal routines ------ */
  278.  
  279. /* The DeskJet can compress (mode 2) */
  280. private int
  281. djet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  282. {    return hpjet_print_page(pdev, prn_stream, DJ, 300, mode_2,
  283.         "\033&k1W\033*b2M");
  284. }
  285. /* The DeskJet500 can compress (modes 2&3) */
  286. private int
  287. djet500_print_page(gx_device_printer *pdev, FILE *prn_stream)
  288. {    return hpjet_print_page(pdev, prn_stream, DJ500, 300, mode_3,
  289.         "\033&k1W");
  290. }
  291. /* The LaserJet series II can't compress */
  292. private int
  293. ljet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  294. {    return hpjet_print_page(pdev, prn_stream, LJ, 300, mode_0,
  295.         "\033*b0M");
  296. }
  297. /* The LaserJet Plus can't compress */
  298. private int
  299. ljetplus_print_page(gx_device_printer *pdev, FILE *prn_stream)
  300. {    return hpjet_print_page(pdev, prn_stream, LJplus, 300, mode_0,
  301.         "\033*b0M");
  302. }
  303. /* LaserJet series IIp & IId compress (mode 2) */
  304. /* but don't support *p+ or *b vertical spacing. */
  305. private int
  306. ljet2p_print_page(gx_device_printer *pdev, FILE *prn_stream)
  307. {    return hpjet_print_page(pdev, prn_stream, LJ2p, 300, mode_2p,
  308.         "\033*r0F\033*b2M");
  309. }
  310. /* All LaserJet series IIIs (III,IIId,IIIp,IIIsi) compress (modes 2&3) */
  311. private int
  312. ljet3_print_page(gx_device_printer *pdev, FILE *prn_stream)
  313. {    return hpjet_print_page(pdev, prn_stream, LJ3, 300, mode_3,
  314.         "\033*r0F");
  315. }
  316. /* LaserJet IIId is same as LaserJet III, except for duplex */
  317. private int
  318. ljet3d_print_page(gx_device_printer *pdev, FILE *prn_stream)
  319. {    return hpjet_print_page(pdev, prn_stream, LJ3D, 300, mode_3,
  320.         "\033*r0F");
  321. }
  322. /* LaserJet 4 series compresses, and it needs a special sequence to */
  323. /* allow it to specify coordinates at 600 dpi. */
  324. private int
  325. ljet4_print_page(gx_device_printer *pdev, FILE *prn_stream)
  326. {    int dots_per_inch = (int)pdev->y_pixels_per_inch;
  327.     char real_init[50];
  328.     sprintf(real_init, "\033*r0F\033&u%dD", dots_per_inch);
  329.     return hpjet_print_page(pdev, prn_stream, LJ4, dots_per_inch, mode_3,
  330.         real_init);
  331. }
  332. /* The 2563B line printer can't compress */
  333. /* and doesn't support *p+ or *b vertical spacing. */
  334. private int
  335. lp2563_print_page(gx_device_printer *pdev, FILE *prn_stream)
  336. {    return hpjet_print_page(pdev, prn_stream, LP2563B, 300, mode_0ns,
  337.         "\033*b0M");
  338. }
  339. /* The Oce line printer has TIFF compression */
  340. /* and doesn't support *p+ or *b vertical spacing. */
  341. private int
  342. oce9050_print_page(gx_device_printer *pdev, FILE *prn_stream)
  343. {    int code;
  344.  
  345.     /* Switch to HP_RTL. */
  346.     fputs("\033%1B", prn_stream);    /* Enter HPGL/2 mode */
  347.     fputs("BP", prn_stream);    /* Begin Plot */
  348.     fputs("IN;", prn_stream);    /* Initialize (start plot) */
  349.     fputs("\033%1A", prn_stream);    /* Enter PCL mode */
  350.  
  351.     code = hpjet_print_page(pdev, prn_stream, OCE9050, 400, mode_3ns,
  352.                 "\033*b3M");
  353.  
  354.     /* Return to HPGL/2 mode. */
  355.     fputs("\033%1B", prn_stream);    /* Enter HPGL/2 mode */
  356.     if ( code == 0 )
  357.       {    fputs("PU", prn_stream);    /* Pen Up */
  358.         fputs("SP0", prn_stream);    /* Pen Select */
  359.         fputs("PG;", prn_stream);    /* Advance Full Page */
  360.         fputs("\033E", prn_stream);    /* Reset */
  361.       }
  362.  
  363.     return code;
  364. }
  365.  
  366. /* Send the page to the printer.  For speed, compress each scan line, */
  367. /* since computer-to-printer communication time is often a bottleneck. */
  368. private int
  369. hpjet_print_page(gx_device_printer *pdev, FILE *prn_stream, int ptype,
  370.   int dots_per_inch, compression_modes cmodes, const char *page_init)
  371. {    int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  372.     int line_size_words = (line_size + W - 1) / W;
  373.     uint storage_size_words = line_size_words * 8; /* data, out_row, out_row_alt, prev_row */
  374.     word *storage = (ulong *)gs_malloc(storage_size_words, W,
  375.                        "hpjet_print_page");
  376.     word
  377.       *data_words,
  378.       *out_row_words,
  379.       *out_row_alt_words,
  380.       *prev_row_words;
  381. #define data ((byte *)data_words)
  382. #define out_row ((byte *)out_row_words)
  383. #define out_row_alt ((byte *)out_row_alt_words)
  384. #define prev_row ((byte *)prev_row_words)
  385.     byte *out_data;
  386.     int x_dpi = pdev->x_pixels_per_inch;
  387.     int y_dpi = pdev->y_pixels_per_inch;
  388.     int y_dots_per_pixel = dots_per_inch / y_dpi;
  389.     int num_rows = dev_print_scan_lines(pdev);
  390.  
  391.     int out_count;
  392.     int compression = -1;
  393.     static const char *from2to3 = "\033*b3M";
  394.     static const char *from3to2 = "\033*b2M";
  395.     int penalty_from2to3 = strlen(from2to3);
  396.     int penalty_from3to2 = strlen(from3to2);
  397.     int paper_size = gdev_pcl_paper_size((gx_device *)pdev);
  398.     int code = 0;
  399.     bool dup = pdev->Duplex;
  400.     bool dupset = pdev->Duplex_set >= 0;
  401.  
  402.     if ( storage == 0 )    /* can't allocate working area */
  403.         return_error(gs_error_VMerror);
  404.     data_words = storage;
  405.     out_row_words = data_words + (line_size_words * 2);
  406.     out_row_alt_words = out_row_words + (line_size_words * 2);
  407.     prev_row_words = out_row_alt_words + (line_size_words * 2);
  408.     /* Clear temp storage */
  409.     memset(data, 0, storage_size_words * W);
  410.  
  411.     /* Initialize printer. */
  412.     if ( pdev->PageCount == 0 )
  413.       {    fputs("\033E", prn_stream);        /* reset printer */
  414.         /* If the printer supports it, set the paper size */
  415.         /* based on the actual requested size. */
  416.         if ( !(ptype == LJ || ptype == LJplus ) )
  417.         {    fprintf(prn_stream, "\033&l%dA", paper_size);
  418.         }
  419.         /* If printer can duplex, set duplex mode appropriately. */
  420.         if ( ptype == LJ3D )
  421.         {       if ( dupset && dup )
  422.               fputs("\033&l1S",prn_stream);
  423.                 else if ( dupset && !dup )
  424.               fputs("\033&l0S",prn_stream);
  425.                 else    /* default to duplex for this printer */
  426.               fputs("\033&l1S",prn_stream);
  427.         }
  428.     }
  429.  
  430.         /* Put out per-page initialization. */
  431.     fputs("\033&l0o0l0E", prn_stream);
  432.     fputs(page_init, prn_stream);
  433.  
  434.         /* End raster graphics, position cursor at top. */
  435.     fputs("\033*rB\033*p0x0Y", prn_stream);
  436.  
  437.         /* The DeskJet and DeskJet Plus reset everything upon */
  438.         /* receiving \033*rB, so we must reinitialize graphics mode. */
  439.     if ( ptype == DJ ) 
  440.           fputs(page_init, prn_stream);
  441.  
  442.         /* Set resolution. */
  443.     fprintf(prn_stream, "\033*t%dR", x_dpi);
  444.  
  445.     /* Send each scan line in turn */
  446.        {    int lnum;
  447.         int num_blank_lines = 0;
  448.         word rmask = ~(word)0 << (-pdev->width & (W * 8 - 1));
  449.  
  450.         /* Transfer raster graphics. */
  451.         for ( lnum = 0; lnum < num_rows; lnum++ )
  452.            {    register word *end_data =
  453.                 data_words + line_size_words;
  454.             code = gdev_prn_copy_scan_lines(pdev, lnum,
  455.                          (byte *)data, line_size);
  456.             if ( code < 0 )
  457.                 break;
  458.                /* Mask off 1-bits beyond the line width. */
  459.             end_data[-1] &= rmask;
  460.             /* Remove trailing 0s. */
  461.             while ( end_data > data_words && end_data[-1] == 0 )
  462.               end_data--;
  463.             if ( end_data == data_words )
  464.                {    /* Blank line */
  465.                 num_blank_lines++;
  466.                 continue;
  467.                }
  468.  
  469.             /* We've reached a non-blank line. */
  470.             /* Put out a spacing command if necessary. */
  471.             if ( num_blank_lines == lnum )
  472.             {    /* We're at the top of a page. */
  473.                 if ( cmodes == mode_2p || cmodes == mode_0ns )
  474.                 {    /* Start raster graphics. */
  475.                     fputs("\033*r1A", prn_stream);
  476.                     for ( ; num_blank_lines; num_blank_lines-- )
  477.                         fputs("\033*bW", prn_stream);
  478.                 }
  479.                 else if ( cmodes == mode_3ns )
  480.                 {    /* Start raster graphics. */
  481.                     fputs("\033*r1A", prn_stream);
  482. #if 1    /* don't waste paper */
  483.                     if ( num_blank_lines > 0 )
  484.                         fputs("\033*b0W", prn_stream);
  485.                     num_blank_lines = 0;
  486. #else
  487.                     for ( ; num_blank_lines; num_blank_lines-- )
  488.                         fputs("\033*b0W", prn_stream);
  489. #endif
  490.                 }
  491.                 else
  492.                 {    if ( num_blank_lines > 0 )
  493.                       fprintf(prn_stream, "\033*p+%dY",
  494.                         num_blank_lines * y_dots_per_pixel);
  495.                     /* Start raster graphics. */
  496.                     fputs("\033*r1A", prn_stream);
  497.                 }
  498.             }
  499.             /* Skip blank lines if any */
  500.             else if ( num_blank_lines != 0 )
  501.             { /* For Canon LBP4i and some others: */
  502.               /* <ESC>*b<n>Y doesn't properly clear the seed */
  503.               /* row if we are in compression mode 3. */
  504.               if ( (num_blank_lines < MIN_SKIP_LINES &&
  505.                 compression != 3) ||
  506.                 cmodes == mode_3ns ||
  507.                 cmodes == mode_2p || cmodes == mode_0ns
  508.                   )
  509.                {    /* Moving down from current position */
  510.                 /* causes head motion on the DeskJet, so */
  511.                 /* if the number of lines is small, */
  512.                 /* we're better off printing blanks. */
  513.                 if ( cmodes == mode_3ns && compression != 2 )
  514.                 {    /* Switch to mode 2 */
  515.                     fputs(from3to2, prn_stream);
  516.                     compression = 2;
  517.                 }
  518.                 if ( cmodes == mode_3 || cmodes == mode_3ns )
  519.                 {    /* Must clear the seed row. */
  520.                     fputs("\033*b1Y", prn_stream);
  521.                     num_blank_lines--;
  522.                 }
  523.                 if ( cmodes == mode_3ns )
  524.                 {
  525.                     for ( ; num_blank_lines; num_blank_lines-- )
  526.                         fputs("\033*b0W", prn_stream);
  527.                 }
  528.                 else
  529.                    {
  530.                     for ( ; num_blank_lines; num_blank_lines-- )
  531.                         fputs("\033*bW", prn_stream);
  532.                 }
  533.                }
  534.                else if ( cmodes == mode_0 )    /* PCL 3 */
  535.                {    fprintf(prn_stream, "\033*p+%dY",
  536.                     num_blank_lines * y_dots_per_pixel);
  537.                }
  538.                else
  539.                {       fprintf(prn_stream, "\033*b%dY",
  540.                        num_blank_lines);
  541.                }
  542.                /* Clear the seed row (only matters for */
  543.                /* mode 3 compression). */
  544.                memset(prev_row, 0, line_size);
  545.             }
  546.             num_blank_lines = 0;
  547.  
  548.             /* Choose the best compression mode */
  549.             /* for this particular line. */
  550.             switch (cmodes)
  551.               {
  552.               case mode_3:
  553.               case mode_3ns:
  554.                {    /* Compression modes 2 and 3 are both */
  555.                 /* available.  Try both and see which one */
  556.                 /* produces the least output data. */
  557.                 int count3 = gdev_pcl_mode3compress(line_size, data,
  558.                                prev_row, out_row);
  559.                 int count2 = gdev_pcl_mode2compress(data_words, end_data,
  560.                                out_row_alt);
  561.                 int penalty3 =
  562.                   (compression == 3 ? 0 : penalty_from2to3);
  563.                 int penalty2 =
  564.                   (compression == 2 ? 0 : penalty_from3to2);
  565.                 if ( count3 + penalty3 < count2 + penalty2)
  566.                    {    if ( compression != 3 )
  567.                         fputs(from2to3, prn_stream);
  568.                     compression = 3;
  569.                     out_data = out_row;
  570.                     out_count = count3;
  571.                    }
  572.                 else
  573.                    {    if ( compression != 2 )
  574.                         fputs(from3to2, prn_stream);
  575.                     compression = 2;
  576.                     out_data = out_row_alt;
  577.                     out_count = count2;
  578.                    }
  579.                 break;
  580.                }
  581.               case mode_2:
  582.               case mode_2p:
  583.                 out_data = out_row;
  584.                    out_count = gdev_pcl_mode2compress(data_words, end_data,
  585.                               out_row);
  586.                 break;
  587.               default:
  588.                 out_data = data;
  589.                 out_count = (byte *)end_data - data;
  590.               }
  591.  
  592.             /* Transfer the data */
  593.             fprintf(prn_stream, "\033*b%dW", out_count);
  594.             fwrite(out_data, sizeof(byte), out_count,
  595.                    prn_stream);
  596.            }
  597.     }
  598.  
  599.     /* end raster graphics and eject page */
  600.     fputs("\033*rB\f", prn_stream);
  601.         
  602.     /* free temporary storage */
  603.     gs_free((char *)storage, storage_size_words, W, "hpjet_print_page");
  604.  
  605.     return code;
  606. }
  607.